<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>文档管理</title>
        <link rel="stylesheet" href="../../component/ui/css/ui.css"/>
    </head>
    <body class="pear-container">

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="power-table" lay-filter="power-table"></table>
            </div>
        </div>

        <script type="text/html" id="power-toolbar">
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
                新增
            </button>
            <button class="pear-btn pear-btn-success pear-btn-md" lay-event="back">
                <i class="layui-icon layui-icon-senior"></i>
                回写
            </button>
        </script>

        <script type="text/html" id="power-bar">
            {{# if(d.type == 1){ }}
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
                <i class="layui-icon layui-icon-edit"></i>
            </button>
            {{# } }}
            <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
                <i class="layui-icon layui-icon-delete"></i>
            </button>
        </script>
        <script type="text/html" id="doc-time">
			{{layui.util.toDateString(d.add_time*1000, 'yyyy-MM-dd HH:mm:ss')}}
		</script>
		<script type="text/html" id="up-time">
			{{layui.util.toDateString(d.update_time*1000, 'yyyy-MM-dd HH:mm:ss')}}
		</script>
        <script src="../../config/config.js"></script>
        <script src="../../component/layui/layui.js"></script>
        <script src="../../component/ui/ui.js"></script>
        <script>
            layui.use([
                'table', 'form', 'jquery', 'treetable'
            ], function () {
                let table = layui.table;
                let form = layui.form;
                let $ = layui.jquery;
                let treetable = layui.treetable,
                    req = _req();

                let MODULE_PATH = "./";

                window.render = function () {
                    treetable.render({
                        treeColIndex: 1,
                        treeSpid: 0,
                        treeIdName: 'id',
                        treePidName: 'pid',
                        skin: 'line',
                        treeDefaultClose: false,
                        toolbar: '#power-toolbar',
                        elem: '#power-table',
                        url: 'doc/listmd?did=' + req.did,
                        page: false,
                        cols: [
                            [
                                {
                                    field: 'id',
                                    title: 'ID'
                                }, {
                                    field: 'title',
                                    minWidth: 200,
                                    edit: 'text',
                                    title: '名称'
                                }, {
                                    field: 'order_num',
                                    minWidth: 90,
                                    edit: 'text',
                                    title: '排序'
                                }, {
                                    field: 'name',
                                    title: '路径'
                                }, {
                                    title: '添加时间',
                                    field: 'add_time',
                                    align: 'center',
                                    templet: '#doc-time'
                                },
                                {
                                    title: '更新时间',
                                    field: 'update_time',
                                    align: 'center',
                                    templet: '#up-time'
                                }, {
                                    title: '操作',
                                    templet: '#power-bar',
                                    width: 150,
                                    align: 'center'
                                }
                            ]
                        ]
                    });
                }

                render();

                table.on('tool(power-table)', function (obj) {
                    if (obj.event === 'remove') {
                        window.remove(obj);
                    } else if (obj.event === 'edit') {
                        window.edit(obj);
                    }
                })

                table.on('toolbar(power-table)', function (obj) {
                    if (obj.event === 'add') {
                        window.addmd();
                    } else if (obj.event === 'back') {
                        window.back();
                    } else if (obj.event === 'refresh') {
                        window.refresh();
                    }
                });
                table.on('edit(power-table)', function (obj) {
					//console.log(obj)
					
					let update = {
						id: obj.data.id,
						field: obj.field,
						value: obj.value
					}
                    console.log(update)
                    //return;
					_post(layui, 'doc/editData', update, res => {
						//console.log(res)
						layer.msg('更新成功', {
								icon: 1,
								time: 1000
							});
					}, err => {
						//console.log(err)

					})
				});
                
               window.addmd = function() {
                    parent.layui.admin.addTab('godocmsdocsadd_' + 113 + req.did,
					'添加文档',
					'/admin/view/doc/addmd.html?did=' + req.did)
                    /*
					layer.open({
						type: 2,
						title: '添加文档',
						shade: 0.1,
						offset: 'rt',
						area: ['98%', '100%'],
						anim: 1,
						maxmin: true,
						content: MODULE_PATH + 'addmd.html?did=' + req.did
					});*/
				}

                window.edit = function (obj) {
                    parent.layui.admin.addTab('godocmsdocsedit_' + obj.data.id+111,
					obj.data.title + '-修改文档',
					'/admin/view/doc/editmd.html?id=' + obj.data.id + '&did=' + req.did)
                    //console.log(obj)
                    /*
                    layer.open({
                        type: 2,
                        title: '修改',
                        shade: 0.1,
                        offset: 'rt',
                        area: [
                            '98%', '100%'
                        ],
                        anim: 1,
                        maxmin: true,
                        content: MODULE_PATH + 'editmd.html?id=' + obj.data.id + '&did=' + req.did
                    });*/
                }
                window.back = function (obj) {
                    layer.confirm('回写将按summary.md结构重置数据！', {
                        icon: 3,
                        title: '提示'
                    }, (index) => {
                        layer.close(index);
                        _post(layui, 'doc/back', {
                            did : req.did
                        }, res => {
                            layer.msg('回写成功', {
                                icon: 1,
                                time: 1000
                            }, () => {
                                render();
                            });
                        }, err => {
                            //layer.msg('删除失败',{icon:2,time:1000});
                        })
                    });
                }

                window.remove = function (obj) {
                    layer.confirm('确定要删除该文档', {
                        icon: 3,
                        title: '提示'
                    }, (index) => {
                        layer.close(index);
                        _post(layui, 'doc/delmd', {
                            id: obj.data.id,
                            did : req.did
                        }, res => {
                            layer.msg('删除成功', {
                                icon: 1,
                                time: 1000
                            }, () => {
                                obj.del();
                            });
                        }, err => {
                            //layer.msg('删除失败',{icon:2,time:1000});
                        })
                    });
                }

            })
        </script>
    </body>
</html>